/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifal.crazypizza.bd;

import br.edu.ifal.crazypizza.modelo.Funcionario;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

/**
 *
 * @author ifal
 */
public class FuncionariosBD {
  public void insereFuncionario(Funcionario f) throws SQLException {
        Conexao fc = new Conexao();
        Connection con = fc.getConnection();
        String sql = "insert into Funcionario "
                + "(nome,data_nascimento,estado,cargo,celular,bairro,rua,cidade,numero_conta,cep,codigo) "
                + "values (?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement stmt = con.prepareStatement(sql);

        // preenche os valores
        stmt.setString(1, f.getNome());
        
        

        java.sql.Date dataParaGravar = new java.sql.Date(Calendar.getInstance().getTimeInMillis());
        stmt.setDate(4, dataParaGravar);
        stmt.execute();
        stmt.close();
        con.close();

    }

    public void altera(Funcionario f) {
        Conexao fc = new Conexao();
        Connection con = fc.getConnection();
        String sql = "update contatos set nome=?, email=?,"
                + "endereco=? where id=?";

        try {
            try (PreparedStatement stmt = con.prepareStatement(sql)) {
                stmt.setString(1, f.getNome());
                
                //stmt.setDate(4, new Date(contato.getDataNascimento().getTimeInMillis()));
                stmt.setLong(4, f.getId());
                stmt.execute();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public Funcionario buscaContatoPeloId(int id) throws SQLException {
        Conexao fc = new Conexao();
        Connection con = fc.getConnection();
        String sql = "select * from Funcionarios where id = ?";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setInt(1, id);
        ResultSet rs = stmt.executeQuery();
        Funcionario c1 = new Funcionario();
        while (rs.next()) {
            
            c1.setNome(rs.getString("nome"));
            c1.setEstado(rs.getString("estado"));
            c1.setRua(rs.getString("rua"));
            c1.setBairro(rs.getString("bairro"));
            c1.setCidade(rs.getString("cidade"));
            c1.setNumero_conta(rs.getString("numero_conta"));
            c1.setCargo(rs.getString("cargo"));
            c1.setCep(rs.getString("cep"));
            c1.setData_nascimento(rs.getString("data_nascimento"));
           
        }
        rs.close();
        stmt.close();
        con.close();
        return c1;
    }
}

    

